package com.hzw.code.fast.sys.service.impl;

import com.hzw.code.fast.sys.controller.args.SysRoleArgs;
import com.hzw.code.fast.sys.controller.args.SysRolePageArgs;
import com.hzw.code.fast.sys.controller.args.SysRoleQueryArgs;
import com.hzw.code.fast.sys.dto.SysRoleDto;
import com.hzw.code.fast.sys.dto.page.SysRolePageDto;
import com.hzw.code.fast.sys.entity.SysRoleEntity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.code.fast.sys.mapper.SysRoleMapper;
import com.hzw.code.fast.sys.model.SysRoleModel;
import com.hzw.code.fast.sys.service.SysRoleService;
import com.hzw.code.common.utils.ActionException;
import com.hzw.code.common.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色表(SysRole)业务层实现
 *
 * @author 胡汉三
 * @date 2020-05-20 14:24:43
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {
    private final SysRoleMapper mapper;


    @Override
    public void save(SysRoleArgs args) throws RuntimeException {
        SysRoleEntity entity = new SysRoleEntity();
        BeanUtils.copyProperties(args, entity);
        boolean result = super.save(entity);
        if (!result) {
            throw new ActionException(ResultCodeEnum.DATABASE_INSERT_ERROR);
        }
    }


    @Override
    public void updateById(SysRoleArgs args) throws RuntimeException {
        // 查询数据是否存在
        this.getById(args.getRoleId());
        // 更新数据
        SysRoleEntity entity = new SysRoleEntity();
        BeanUtils.copyProperties(args, entity);
        boolean result = super.updateById(entity);
        if (!result) {
            throw new ActionException(ResultCodeEnum.DATABASE_UPDATE_ERROR);
        }
    }


    @Override
    public List<SysRoleDto> selectListByModel(SysRoleQueryArgs args) throws RuntimeException {
        List<SysRoleDto> list = mapper.selectListByModel(args);
        if (list == null || list.isEmpty()) {
            throw new ActionException(ResultCodeEnum.DATABASE_NODATA);
        }
        return list;
    }


    @Override
    public IPage<SysRolePageDto> page(SysRolePageArgs args) throws RuntimeException {
        return mapper.page(args.getQueryPage(), args);
    }


    @Override
    public SysRoleDto getById(String id) throws RuntimeException {
        SysRoleEntity byId = super.getById(id);
        if (byId == null) {
            throw new ActionException(ResultCodeEnum.DATABASE_NODATA);
        }
        SysRoleDto dto = new SysRoleDto();
        BeanUtils.copyProperties(byId, dto);
        return dto;
    }

    @Override
    public List<SysRoleModel> getRoleCodeByUserId(String userId) {
        return mapper.getRoleCodeByUserId(userId);
    }
}